package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.event.OperateSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.BuildingPersonAdapter;
import com.sxz.sbs.module.client.bean.StaffListBean;
import com.sxz.sbs.module.client.presenter.StaffListPresenter;
import com.sxz.sbs.module.client.view.StaffView;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * @author hp
 * @date 2018/1/10
 * 楼号分配给人员
 */
public class BuildingPersonnelActivity extends BaseActivity<StaffListPresenter> implements StaffView{

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.lv_buildingperson)
    RecyclerView lvBuildingPerson;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private List<StaffListBean> staffListBean = new ArrayList<>();
    private BuildingPersonAdapter myAdapter;
    private List<String> list=new ArrayList<>();
    private int parkingId;
    private int mangerId;
    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        new CommonTitle.Builder(this).setTitle(getIntent().getStringExtra("parkingname")+"员工").build().show();
        parkingId = getIntent().getIntExtra("parkingid",0);
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(true);
        getData();
        lvBuildingPerson.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new BuildingPersonAdapter(staffListBean);
        lvBuildingPerson.setAdapter(myAdapter);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        lvBuildingPerson.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                mangerId = staffListBean.get(position).getId();
                list.add(staffListBean.get(position).getTxt_Name());
                myAdapter.setSelectPosition(position);
                myAdapter.notifyDataSetChanged();

            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getData();
            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_buildingpersonnel;
    }

    @Override
    protected StaffListPresenter createPresenter() {
        return new StaffListPresenter();
    }


    @OnClick({R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                if (list.size()>0) {
                    Intent intent = new Intent(BuildingPersonnelActivity.this, BuildingActivity.class);
                    intent.putExtra("parkingid", parkingId);
                    intent.putExtra("mangerId",mangerId);
                    startActivity(intent);
                }else {
                    ToastUtils.showShort("请选择员工");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onStaffListSuccess(List<StaffListBean> data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data != null && data.size() >0) {
                stateLayout.showContent();
                staffListBean.clear();
                staffListBean.addAll(data);
                myAdapter.notifyDataSetChanged();
            } else {
                staffListBean.clear();
                myAdapter.notifyDataSetChanged();
                //  数据为空
                stateLayout.showEmpty();
            }
        }
    }

    private void getData(){
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("parkingId", parkingId);
        mPresenter.getStaffList(map);
    }
    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        list.clear();
        myAdapter.setSelectPosition(-1);
        myAdapter.notifyDataSetChanged();
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showEmpty();
        }
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOperateSuccessEvent(OperateSuccessEvent event) {
       // finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
